#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <vector>
#include <string>
#include <cstdlib>
#include "lsMP3.h"

using namespace std;

int lsMP3(string path,vector<string> &VT)
{
	path=path+"/";
	DIR * dir;
	string sub;
	dir=opendir(path.c_str());
	if(dir==NULL)
	{
		cout<<path<<endl;
		cout<<"failed to open dir"<<endl;
		return 0;
	}
	struct dirent *p;
	while(true)
	{
		p=readdir(dir);
		if(p==NULL) break;
		if(p->d_type==DT_DIR&&(string)p->d_name!="."&&(string)p->d_name!="..")
		{
			//cout<<p->d_name<<endl;
			//path=path+(string)p->d_name;
			lsMP3(path+(string)p->d_name,VT);
		}
		sub=p->d_name;
		if(sub.length()<4) continue;
		sub=sub.substr(sub.length()-4,4);
		if(sub==".mp3")
			VT.push_back(path+(string)p->d_name);
	}
	//cout<<"before"<<endl;
	closedir(dir);
	return 0;
}

#ifdef _USINGMAIN
int main()
{
	vector<string> VT;
	string p;
	p=getenv("HOME");
	lsMP3(p,VT);
	for(vector<string>::iterator it=VT.begin();it!=VT.end();it++)
	{
		cout<<*it<<endl;
	}
	return 0;
}
#endif
